package com.ziyun.erp.modules.material.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.utility.New;
import com.ziyun.erp.modules.material.dao.StoreStockMaterialDao;
import com.ziyun.erp.modules.material.entity.ErpStockEntity;
import com.ziyun.erp.modules.material.entity.MaterialEntity;
import com.ziyun.erp.modules.material.entity.storeStockMaterialEntity;
import com.ziyun.erp.modules.material.service.ErpStockService;
import com.ziyun.erp.modules.material.service.MaterialService;
import com.ziyun.erp.modules.material.service.StoreStockMaterialService;
import com.ziyun.erp.modules.supp.entity.SellerInfoEntity;
import com.ziyun.erp.modules.supp.service.SellerInfoService;
import io.renren.common.exception.RRException;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.sql.Wrapper;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author ZhaoWei
 * @Date 2018/11/08 08:59
 * @Version V1.0
 */

@Service("storeStockMaterialService")
public class StoreStockMaterialServiceImpl extends ServiceImpl<StoreStockMaterialDao,storeStockMaterialEntity> implements StoreStockMaterialService {

	@Autowired
	private ErpStockService erpStockService;

	@Autowired
	private MaterialService materialService;

	@Autowired
	private StoreStockMaterialService storeStockMaterialService;

	@Override
	public int insert(Integer stockId, String materialId, BigDecimal materialQuantity, BigDecimal cost, BigDecimal money, String unit, String remark) {
		return baseMapper.insertStoreStockMaterial(stockId, materialId, materialQuantity, cost, money, unit, remark);
	}


	@Override
	public List<SellerInfoEntity> selectCostByMaterialId(Integer materialId) {
		List<SellerInfoEntity> list = new ArrayList();
		List<storeStockMaterialEntity> storeStockMaterialEntities = baseMapper.selectCostAndStockIdByMaterialId(materialId);
		List<SellerInfoEntity> mdList = baseMapper.getMDList();
		for (SellerInfoEntity infoEntity : mdList) {
			for (storeStockMaterialEntity storeStockMaterialEntity : storeStockMaterialEntities) {
				Integer stockId = storeStockMaterialEntity.getStockId();
				ErpStockEntity erpStockEntity = baseMapper.getErpStockEntity(stockId);
				String institution = erpStockEntity.getInstitution();
				SellerInfoEntity sellerInfoEntity = baseMapper.getSellerInfoEntity(institution);
				if (infoEntity.getSellerId().equals(sellerInfoEntity.getSellerId())) {
					infoEntity.setCost(storeStockMaterialEntity.getCost());
				}
			}
			list.add(infoEntity);
		}
		return list;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public R saveOrInsert(MaterialEntity material, @NotNull List<SellerInfoEntity> sellerInfoEntity) {
		ValidatorUtils.validateEntity(material);
		materialService.updateAllColumnById(material);
		String materialId = material.getMaterialId().toString();
		for (SellerInfoEntity infoEntity : sellerInfoEntity) {
			Integer sellerId = infoEntity.getSellerId();
			BigDecimal cost = infoEntity.getCost();
			if(cost!=null){
				Integer ErpstockId = erpStockService.getStockId(sellerId);
				Integer StoreStockMaterialStockId = storeStockMaterialService.getStockId(ErpstockId,materialId);
				if(StoreStockMaterialStockId!=null){
					try {
						storeStockMaterialService.updateCost(materialId,StoreStockMaterialStockId,cost);
					} catch (Exception e) {
						throw new RuntimeException();
					}
				}else{
					try{
						storeStockMaterialService.insertCost(materialId, ErpstockId, cost);
					} catch (Exception e){
						throw new RuntimeException();
					}
				}
			}
		}
		return R.ok();
	}

	@Override
	public void checkOutUpdate(Integer stockMaterialId, BigDecimal tempResult) {
		baseMapper.checkOutUpdate(stockMaterialId,tempResult);
	}

	@Override
	public void checkInUpdate(Integer stockId, String materialId, BigDecimal checkResult) {
		Integer integer = baseMapper.checkInUpdate(stockId, materialId, checkResult);
		if(integer.intValue() > 1){
			throw new RRException("一个门店下有2个相同的材料id且均为活动状态");
		}
	}

	@Override
	public Integer getStockId(Integer ErpstockId,String materialId) {
		return baseMapper.getStockId(ErpstockId,materialId);
	}

	@Override
	public int updateCost(String materialId, Integer storeStockMaterialStockId, BigDecimal cost) {
		return baseMapper.updateCost(materialId,storeStockMaterialStockId,cost);
	}

	@Override
	public int insertCost(String materialId, Integer erpstockId, BigDecimal cost) {
		return baseMapper.insertCost(materialId,erpstockId,cost);
	}
}

